package com.example.pkmedical.Activity.servicecode;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.pkmedical.APP.MyApp;
import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.Event.MessageWrap;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.DistinguishUtil;
import com.example.pkmedical.Utils.SerialProtUtil;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CalibrationSensorActivity extends BaseActivity {

    private static final String TAG = "yangshuo";
    @BindView(R.id.censor_img)
    ImageView censorImg;
    @BindView(R.id.censor_text)
    TextView censorText;
    @BindView(R.id.censor_ok)
    Button censorOk;
    @BindView(R.id.censor_no)
    Button censorNo;
    byte[] mBytes;
    private int mNum = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calibrationsensor);
        ButterKnife.bind(this);
    }


    @Override
    public void initData() {

    }

    @Override
    public void initView() {

    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void showDialog(String... msg) {

    }

    @Override
    public void onReceiveEvent(MessageWrap event) {
        super.onReceiveEvent(event);
        if (event.getCode() == MessageWrap.SENSOR_WEIGHT) {
            Log.d(TAG, "onDataReceivedSensor [ byte[] ]: " + Arrays.toString(event.getBytes()));
            mBytes = event.getBytes();
            distinguish(mBytes);
        } else {
            Log.d(TAG, "onDataReceivedSensor [ byte[] ]: NoThisData");
        }
    }

    /**
     * 返回数据识别
     */
    private void distinguish(byte[] bytes) {


        if (DistinguishUtil.distinguishUtil(bytes)) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    switch (bytes[3]) {
                        case 0x01:
                            mNum = 2;
                            censorText.setText(R.string.sensor_two);
                            censorImg.setImageResource(R.drawable.img_sensor_two);
                            break;
                        case 0x02:
                            mNum = 3;
                            censorText.setText(R.string.sensor_three);
                            censorImg.setImageResource(R.drawable.img_sensor_three);
                            break;
                        case 0x03:
                            mNum = 4;
                            censorText.setText(R.string.sensor_four);
                            censorImg.setImageResource(R.drawable.img_sensor_four);
                            break;
                        case 0x04:
                            mNum = 5;
                            censorText.setText(R.string.sensor_all);
                            censorImg.setImageResource(R.drawable.img_sensor_five);
                            break;
                        case 0x05:
                            startActivity(new Intent(MyApp.getAppContext(), SensorSuccessActivity.class));
//                            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                            CalibrationSensorActivity.this.finish();
                            break;
                    }
                }
            });
        } else {
        }
    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick({R.id.censor_ok, R.id.censor_no})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.censor_ok:
                switch (mNum) {
                    case 1:
                        SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.SENCOR_ONE);
                        break;
                    case 2:
                        SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.SENCOR_TWO);
                        break;
                    case 3:
                        SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.SENCOR_THREE);
                        break;
                    case 4:
                        SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.SENCOR_FOUR);
                        break;
                    case 5:
                        SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.SENCOR_ALL);
                        break;

                }

                break;
            case R.id.censor_no:
                CalibrationSensorActivity.this.finish();
                break;
        }
    }
}
